package com.yequan.hbb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.yequan.hbb.R;
import com.yequan.hbb.customview.ConerImageView;
import com.yequan.hbb.entries.EntrySearch;
import com.yequan.hbb.entries.QuestionSearch;
import com.yequan.hbb.entries.SearchEntry;
import com.yequan.hbb.utils.CornerImageUtils;
import com.yequan.hbb.utils.ImageUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/14.
 */

public class SearchAdapter<D> extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {


    private Context context;
    private List<D> mList;

    public SearchAdapter(Context context) {
        this.context = context;
        mList = new ArrayList<>();
    }

    public void add(int index, D o) {
        mList.add(index, o);
        notifyDataSetChanged();
    }
    public void clear(){
        mList.clear();
        notifyDataSetChanged();
    }
    public void addAll_clear(Collection<D> d) {
        mList.clear();
        mList.addAll(d);
        notifyDataSetChanged();
    }
    public void addAll(Collection<D> d) {
        mList.addAll(d);
        notifyDataSetChanged();
    }
    public void add( D o) {
        mList.add( o);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        Object o = mList.get(position);
        if (o instanceof EntrySearch.DataBeanH.SearchBean.ListBean.RecipeBean.DataBean) {
            return R.layout.wl_l_hp_type_item;
        } else if (o instanceof EntrySearch.DataBeanH.SearchBean.ListBean.QuestionBean) {
            return 0;
        } else if (o instanceof EntrySearch.DataBeanH.SearchBean.ListBean.ClientBean) {
            return 0;
        }
        return super.getItemViewType(position);
    }

    @Override
    public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(context).inflate(viewType, parent,false);
        return new SearchViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(SearchViewHolder holder, int position) {

        /*holder.itemView.setTag(true);
        if (holder.itemView.getTag()!=null){
            return ;
        }*/
        Object o = mList.get(position);
        if (o instanceof EntrySearch.DataBeanH.SearchBean.ListBean.RecipeBean.DataBean) {
            EntrySearch.DataBeanH.SearchBean.ListBean.RecipeBean.DataBean dataBean = (EntrySearch.DataBeanH.SearchBean.ListBean.RecipeBean.DataBean) o;
            holder.iv_left.setImageURI(dataBean.getCoverImage());
          //  CornerImageUtils.loadImage(holder.iv_left,dataBean.getCoverImage(),R.mipmap.default_error);
            holder.tv_1_content.setText(dataBean.getCoverTitle());
            holder.tv_2_content.setText(dataBean.getClientName());
            holder.tv_3_content.setText(String.format("%d个步骤",dataBean.getStep().size()));
            holder.tv_4_content.setText(String.format(Locale.CHINA,"%d次浏览-%d人收藏-%d人分享",dataBean.getVisitNum(),dataBean.getCollectNum(),dataBean.getShareNum()));
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class SearchViewHolder extends RecyclerView.ViewHolder {

        private final SimpleDraweeView iv_left;
        private final TextView tv_1_content;
        private final TextView tv_2_content;
        private final TextView tv_3_content;
        private final TextView tv_4_content;
        public SearchViewHolder(View itemView) {
            super(itemView);
            // itemView.findViewById(R.id.)
            iv_left = ((SimpleDraweeView) itemView.findViewById(R.id.iv_content_left));
            tv_1_content = ((TextView) itemView.findViewById(R.id.tv1_content));
            tv_2_content = ((TextView) itemView.findViewById(R.id.tv2_content));
            tv_3_content = ((TextView) itemView.findViewById(R.id.tv3_content));
            tv_4_content = ((TextView) itemView.findViewById(R.id.tv4_content));
        }
    }
}
